//============================================================================
// Name        : Labelling.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include "AlphaExpansion.h"
#include "Disparity.h"
#include "Segmentation.h"
#include "verify.h"


int main(int args,char ** argv)
{
    setfore(white);

    //CVImage im(cvLoadImage("lenna_smaller_gray.tiff"));
    //CVImage im(cvLoadImage("very_small.tiff"));
    if (args==2)
    {
        try
        {
            CVImage im(cvLoadImage(*(argv+1),CV_LOAD_IMAGE_GRAYSCALE));
            im.ToGrayscale();
            im.ShowImage(*(argv+1));
            vector<int> BW;
            BW.push_back(0);
            BW.push_back(31);
            BW.push_back(63);
            BW.push_back(95);
            BW.push_back(127);
            BW.push_back(159);
            BW.push_back(191);
            BW.push_back(255);
            Segmentation energy(im,16);
            AlphaExpansion a(energy,&im,&BW);
            while (!a.iscomplete())
                a.next_iteration();
            im=a.next_iteration();
            im.FROM_32S_TO_8U();
            string ltitle=*(argv+1);
            im.ShowImage("labelling");
            im.SaveAs("result.png");
        }
        catch (...)
        {
            return -1;
        }
    }
    else
    {
        if (args==3)
        {
            CVImage im1(cvLoadImage(*(argv+1),CV_LOAD_IMAGE_GRAYSCALE));
            CVImage im2(cvLoadImage(*(argv+2),CV_LOAD_IMAGE_GRAYSCALE));
            im1.ToGrayscale();
            im2.ToGrayscale();
            im1.ShowImage(*(argv+1));
            im2.ShowImage(*(argv+2));
            vector<int> BW;
            BW.push_back(0);
            BW.push_back(3);
            BW.push_back(6);
            BW.push_back(9);
            BW.push_back(12);
            BW.push_back(15);
            BW.push_back(18);
            Disparity energy(im1,im2,10);
            AlphaExpansion a(energy,&im1,&BW);
            while (!a.iscomplete())
                a.next_iteration();
            CVImage *res=a.next_iteration();


            string ltitle=*(argv+1);
            res->ShowImage("labelling of disparity");

            res->SaveAs("result.png");

            cout << "Ready for verification!" << endl;
            CVImage diff=verify(&im1, &im2,res);
            cout << "Verification OK" << endl;
            diff.ShowImage("diff");
            diff.SaveAs("diff.png");
            cout << "Verification OK" << endl;
        }
        else
            cout<<"usage: PixelLabel imagefile"<<endl;
    }
    return 0;
}

